package cn.youyeku.zsxh;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;



import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import cn.youyeku.zsxh.Utils.AbsSuperApplication;
import cn.youyeku.zsxh.Utils.AppConfig;
import cn.youyeku.zsxh.Utils.FileUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.api.myCookies;
import okhttp3.Cookie;


/**
 * Created by wings on 2017/3/15.
 */

public class app extends AbsSuperApplication {
    private static app appContext;
    private static String cookiePath;


    public static List<Cookie> cookies = null;

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.w("创建中");
        appContext = this;
        initApp();

    }

    private void initApp() {
        //read cookies
        cookiePath = getAppPath() + "/" + "Cookie";

        LogUtils.w(cookiePath);
        File f = new File(cookiePath);
        try {
            try {
                if (f.exists()) {
                    cookies = myCookies.toCoookies(FileUtils.readDataFromSD(cookiePath));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        File file = new File(AppConfig.APP_CONFIG);
        if (!file.exists()) {
            file.mkdirs();
            initFitstLoading();
        }

        //read config to mem


        AppConfig.CONF_RECEIVE_MESSAGE = Boolean.parseBoolean(AppConfig.get("CONF_RECEIVE_MESSAGE"));
        AppConfig.CONF_ENABLE_SOUNDS = Boolean.parseBoolean(AppConfig.get("CONF_ENABLE_SOUNDS"));
        AppConfig.CONF_CHECK_UPDATE_FIRST = Boolean.parseBoolean(AppConfig.get("CONF_CHECK_UPDATE_FIRST"));
        AppConfig.CONF_USER_ID = AppConfig.get("CONF_USER_ID");
        AppConfig.CONF_USER_NICK_NAME = AppConfig.get("CONF_USER_NICK_NAME");
        AppConfig.CONF_IS_TEACHER = Boolean.valueOf(AppConfig.get("CONF_IS_TEACHER"));


    }

    /**
     * 第一次登录配置
     */
    private void initFitstLoading() {
        //初始化配置
        Properties ps = AppConfig.getProps();
        ps.setProperty("CONF_RECEIVE_MESSAGE", String.valueOf(true));
        ps.setProperty("CONF_ENABLE_SOUNDS", String.valueOf(true));
        ps.setProperty("CONF_CHECK_UPDATE_FIRST", String.valueOf(true));

        AppConfig.set(ps);


    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        LogUtils.w("application 销毁");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LogUtils.w("配置改动");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        LogUtils.w("内存不足 消除缓存");
    }


    public static String getAppPath() {
        return appContext.getFilesDir().getAbsolutePath();
    }

    public static app getAppContext() {
        return appContext;
    }

    public static String getCookiePath() {
        return cookiePath;
    }

    public static boolean isNullCookies() {
        return cookies == null;
    }


    public static void backgroundThreadShortToast(
            final String msg) {
        final Context context = getAppContext();

        if (context != null && !msg.equals("")) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    public static void backgroundThreadShortToast(
            final int msg_id) {
        final Context context = getAppContext();
        final String msg = getAppContext().getResources().getString(msg_id);
        if (context != null && !msg.equals("")) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}
